package dproxy.application.ui.commands;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Iterator;

import dproxy.application.ui.CommandLineProcessor;
import dproxy.server.cache.bo.CacheBO;
import dproxy.server.cache.dao.DatabaseImplentation;
import dproxy.server.cache.to.RequestResponse;

/**
 * List cache entryes.
 */
public class List implements Command {

    /** Sub-Command that shows how many entryes exist on cache. */
    private static final String SC_SIZE = "size";
    /** Sub-Command that shows the last N entryes from cache. */
    private static final String SC_LAST_N = "last";

    /** Date formatter. */
    private SimpleDateFormat sdf = new SimpleDateFormat();
    
    /** Sub command (command option). */
    private String subCommand;
    /** Number numeric parameter. */
    private int firstNumberParameter;

    /**
     * @see dproxy.application.ui.commands.Command#description()
     */
    public String description() {
        return 
            "   'list'       - Show information about cache contents\n" +
            "                  - Options:\n" +
            "                    " + SC_SIZE + "         - show how many entryes exist on cache\n" +
            "                    " + SC_LAST_N + " N       - show last N entryes from cache";
    }

    /**
     * @see dproxy.application.ui.commands.Command#validate(Collection)
     */
    public void validate(Collection<String> parameters) throws ValidationException {
        
        Iterator <String> itParameters = parameters.iterator();
        if (itParameters.hasNext()) {
            subCommand = itParameters.next();
            if (SC_SIZE.equals(subCommand)) {
                ValidationUtils.validateNoMoreParameters(itParameters);
            } else if (SC_LAST_N.equals(subCommand)) {
                firstNumberParameter = ValidationUtils.getNumberParameter(itParameters);
                ValidationUtils.validateNoMoreParameters(itParameters);
            } else {
                throw new ValidationException();
            }
        } else {
            throw new ValidationException();
        }
        
    }

    /**
     * @see dproxy.application.ui.commands.Command#confirm()
     */
    public boolean confirm() {
        // List operation needs no confirmation
        return true;
    }

    /**
     * @see dproxy.application.ui.commands.Command#execute()
     */
    public void execute() {
        DatabaseImplentation cacheImplementation
            = CommandLineProcessor.getInstance().getCacheImplementation();
        String markBegin = "[";
        String markEnd = "]";
        if (SC_SIZE.equals(subCommand)) {
            int cacheSize = CacheBO.getInstance(cacheImplementation).getCacheSize();
            CommandLineProcessor.getInstance().write("Cache contains " + cacheSize + " entryes");
        } else if (SC_LAST_N.equals(subCommand)) {
            Collection<RequestResponse> rrs = CacheBO.getInstance(cacheImplementation).listLast(firstNumberParameter);
            CommandLineProcessor.getInstance().write("Last " + rrs.size() + " entryes:");
            String header = markBegin + "Index" + markEnd;
            header += "\t";
            header += markBegin + "Date" + markEnd;
            CommandLineProcessor.getInstance().write(header);
            for (RequestResponse rr : rrs) {
                String line = markBegin + rr.getIndex() + markEnd;
                line += "\t" + "\t";
                line += markBegin + sdf.format(rr.getDate()) + markEnd;
                CommandLineProcessor.getInstance().write(line);
            }
        }
    }

}
